package com.sitechasia.webx.components.xtable.toolbar;

import org.extremecomponents.table.core.TableConstants;
import org.extremecomponents.table.core.TableModel;
import com.sitechasia.webx.components.xtable.controller.BuilderConstants;
import org.extremecomponents.table.view.html.BuilderUtils;
import org.extremecomponents.table.view.html.TableActions;
import org.extremecomponents.table.view.html.ToolbarBuilder;

import org.extremecomponents.table.view.html.toolbar.ImageItem;
import org.extremecomponents.table.view.html.toolbar.TextItem;
import org.extremecomponents.table.view.html.toolbar.ToolbarItem;
import org.extremecomponents.table.view.html.toolbar.ToolbarItemUtils;
import org.extremecomponents.util.HtmlBuilder;

public class CustomToolbarI18nBuilder extends ToolbarBuilder {
	public CustomToolbarI18nBuilder(HtmlBuilder html, TableModel model) {
		super(html, model);
	}

	/*
	 * 下拉选择框方式显示需选择的跳转页
	 */
	public void pagesDisplayedDroplist() {
		HtmlBuilder html = getHtmlBuilder();
		TableModel model = getTableModel();
		int totalPages = BuilderUtils.getTotalPages(model);
		int currentPage = model.getLimit().getPage();
		StringBuffer onchange = new StringBuffer();
		onchange.append(new CustomTableActions(model).getPageAction());
		html.select().name("pages");
		html.onchange(onchange.toString());
		html.close();
		html.newline();
		html.tabs(4);
		for (int i = 1; i <= totalPages; i++) {
			html.option().value(String.valueOf(i));
			if (currentPage == i) {
				html.selected();
			}
			html.close();
			html.append(String.valueOf(i));
			html.optionEnd();
		}
		html.newline();
		html.tabs(4);
		html.selectEnd();
	}

	/*
	 * 文本输入框显示需选择的跳转页 location：指示 当页面有多个toolbar有多个文本输入框时，指定所选的输入框
	 */
	public void turnToPage(String location) {
		HtmlBuilder html = getHtmlBuilder();
		TableModel model = getTableModel();
		html.append(model.getMessages().getMessage(
				BuilderConstants.TOOLBAR_TURN_PAGE_TEXT));
		html.input("text").id("textNum" + location).name("pages1").size("2")
				.value("1").maxlength("3").styleClass("input").xclose();
		html.append(model.getMessages().getMessage(
				BuilderConstants.TOOLBAR_PAGE_TEXT));
		ImageItem item7 = new ImageItem();
		item7.setImage(BuilderUtils.getImage(model, "go-1"));
		item7.setDisabledImage(BuilderUtils.getImage(model, "go-2"));
		item7.setStyle("border:none;vertical-align:middle;");
		buildCurPage(html, model, item7, location);
	}

	/*
	 * 文本输入框显示需选择的跳转页 本页面toolbar只有单个输入框
	 */
	public void turnToPage() {
		HtmlBuilder html = getHtmlBuilder();
		TableModel model = getTableModel();
		int totalPages = BuilderUtils.getTotalPages(model);
		html.append(model.getMessages().getMessage(
				BuilderConstants.TOOLBAR_TURNTO_PAGE_TEXT));
		html.nbsp();
		html.input("text").id("textNum").name("pages1").size("2").value("1")
				.maxlength("3").styleClass("input").style(
						"vertical-align:middle;").onkeypress(
						"if (event.keyCode == 13) {javascript:checkpage("
								+ totalPages + ");}").xclose();
		html.nbsp();
		html.append(model.getMessages().getMessage(
				BuilderConstants.TOOLBAR_PAGE_TEXT));
		ImageItem item7 = new ImageItem();
		item7.setImage(BuilderUtils.getImage(model, "go-1"));
		item7.setDisabledImage(BuilderUtils.getImage(model, "go-2"));
		item7.setStyle("border:none;vertical-align:middle;");

		buildCurPage(html, model, item7);

		html.nbsp();
	}

	/*
	 * 只显示一个输入框
	 */
	public void jumpPage() {
		HtmlBuilder html = getHtmlBuilder();
		html.input("text").id("textNum").name("pages1").size("2").value("1")
				.maxlength("3").xclose();
	}

	/*
	 * 当页面有多个toolbar有多个文本输入框时，指定ACTION方法
	 */
	public void buildCurPage(HtmlBuilder html, TableModel model,
			ToolbarItem item, String location) {
		StringBuffer oninput = new StringBuffer();
		oninput.append(new CustomTableActions(model)
				.getPageInputAction(location));
		String action = oninput.toString();
		item.setAction(action);

		int totalPages = BuilderUtils.getTotalPages(model);
		if (totalPages < 1) {
			item.disabled(html);
		} else {

			item.enabled(html, model);
		}
	}

	/*
	 * 本页面toolbar只有单个输入框时，指定ACTION方法
	 *
	 */
	public void buildCurPage(HtmlBuilder html, TableModel model,
			ToolbarItem item) {
		StringBuffer oninput = new StringBuffer();
		oninput.append(new CustomTableActions(model).getPageInputAction());
		String action = oninput.toString();
		item.setAction(action);

		int totalPages = BuilderUtils.getTotalPages(model);
		if (totalPages < 1) {
			item.disabled(html);
		} else {

			item.enabled(html, model);
		}
	}

	/*
	 * 显示：总行数
	 */
	public void totalRows() {
		HtmlBuilder html = getHtmlBuilder();
		TableModel model = getTableModel();
		Integer total = new Integer(model.getLimit().getTotalRows());
		Object[] messageArguments = { total };
		html.append(model.getMessages().getMessage(
				BuilderConstants.TOOLBAR_TOTALROWS, messageArguments));

	}

	/*
	 * 显示：总页数
	 */
	public void curTotalPage() {
		HtmlBuilder html = getHtmlBuilder();
		TableModel model = getTableModel();
		int totalPages = BuilderUtils.getTotalPages(model);
		int currentPage = model.getLimit().getPage();
		if (currentPage > totalPages)
			currentPage = totalPages;
		Object[] messageArguments = { currentPage, totalPages };
		html.append(model.getMessages().getMessage(
				BuilderConstants.TOOLBAR_CURTOTALROWS, messageArguments));
		html.nbsp();

	}

	/*
	 * 显示：上页
	 */
	public void prevPage() {
		HtmlBuilder html = getHtmlBuilder();
		TableModel model = getTableModel();
		TextItem item2 = new TextItem();
		item2.setTooltip(getMessages().getMessage(
				BuilderConstants.TOOLBAR_PREV_PAGE_TOOLTIP));
		item2.setText(getMessages().getMessage(
				BuilderConstants.TOOLBAR_PREV_PAGEA_TEXT));
		ToolbarItemUtils.buildPrevPage(html, model, item2);
		html.nbsp();

	}

	/*
	 * 显示：下页
	 */
	public void nextPage() {
		HtmlBuilder html = getHtmlBuilder();
		TableModel model = getTableModel();
		TextItem item3 = new TextItem();
		item3.setTooltip(getMessages().getMessage(
				BuilderConstants.TOOLBAR_NEXT_PAGE_TOOLTIP));
		item3.setText(getMessages().getMessage(
				BuilderConstants.TOOLBAR_NEXT_PAGEA_TEXT));

		ToolbarItemUtils.buildNextPage(html, model, item3);
		html.nbsp();
	}

	/*
	 * 显示：总共#条,列出第x到第y条
	 */
	public void qtStatus() {
		HtmlBuilder html = getHtmlBuilder();
		TableModel model = getTableModel();
		if (model.getLimit().getTotalRows() == 0) {
			html.append(model.getMessages().getMessage(
					BuilderConstants.STATUSBAR_NO_RESULTS_FOUND));
		} else {
			Integer total = new Integer(model.getLimit().getTotalRows());
			Integer from = new Integer(model.getLimit().getRowStart() + 1);
			Integer to = new Integer(model.getLimit().getRowEnd());
			Object[] messageArguments = { total, from, to };
			html
					.append(model.getMessages().getMessage(
							BuilderConstants.STATUSBAR_RESULTS_FOUND,
							messageArguments));
			html.nbsp();
		}
	}

	/*
	 * 显示：1x 2x 3x 4x 5x 页
	 */
	public void displayPages() {
		TableModel model = getTableModel();
		// 总页数
		int totalPages = BuilderUtils.getTotalPages(model);
		// 当前页
		int currentPage = model.getLimit().getPage();
		// 取得总组数，每组五个，组数从0计
		int totalPageGroup = totalPages / 5;
		// 余数
		int yu = totalPages % 5;
		// totalPages是5的倍数时，总组数-1,余数=5
		if (totalPages > 0 && totalPages % 5 == 0) {
			totalPageGroup -= 1;
			yu = 5;
		}
		// 当前页所在组
		int disPageGroup = currentPage / 5;

		if (currentPage > 0 && currentPage % 5 == 0)
			disPageGroup -= 1;
		/*
		 * 非最后组时，显示5条；最后组时显示余数条
		 */
		if (disPageGroup < totalPageGroup) {
			for (int i = 1; i <= 5; i++) {

				displayPage(5 * disPageGroup + i, currentPage);
			}
		} else {
			for (int i = 1; i <= yu; i++) {
				displayPage(5 * disPageGroup + i, currentPage);
			}
		}

	}

	/*
	 * 显示：x 页
	 */
	public void displayPage(int numPage, int currentPage) {
		HtmlBuilder html = getHtmlBuilder();
		TableModel model = getTableModel();
		TextItem item1 = new TextItem();
				item1.setTooltip(""+numPage );
		item1.setText(Integer.toString(numPage));

		if (numPage == currentPage) {
			html.span().styleClass("pageCurrent").close();
		} else {
			html.span().styleClass("page_number").close();
		}
		String action = new TableActions(model).getPageAction(numPage);
		item1.setAction(action);

		item1.enabled(html, model);
		html.spanEnd();
		html.nbsp();

	}

	/*
	 * 显示：|
	 */
	public void separatorLine() {
		HtmlBuilder html = getHtmlBuilder();
		html.append("|");
		html.nbsp();

	}

	/*
	 * 显示：下拉框选择一页显示的行数
	 */
	public void customRowsDisplayedDroplist() {
		HtmlBuilder html = getHtmlBuilder();
		TableModel model = getTableModel();
		int rowsDisplayed = model.getTableHandler().getTable()
				.getRowsDisplayed();

		int maxRowsDisplayed = model.getTableHandler().getTable()
				.getMaxRowsDisplayed();

		int currentRowsDisplayed = model.getLimit().getCurrentRowsDisplayed();

		html.select().name(
				model.getTableHandler().prefixWithTableId()
						+ TableConstants.ROWS_DISPLAYED);

		StringBuffer onchange = new StringBuffer();
		onchange.append(new TableActions(model).getRowsDisplayedAction());
		html.onchange(onchange.toString());

		html.close();

		html.newline();
		html.tabs(4);

		// default rows
		html.option().value(String.valueOf(rowsDisplayed));
		if (currentRowsDisplayed == rowsDisplayed) {
			html.selected();
		}
		html.close();
		html.append(String.valueOf(rowsDisplayed));
		html.optionEnd();

		// max rows
		html.option().value(String.valueOf(maxRowsDisplayed));
		if (currentRowsDisplayed == maxRowsDisplayed) {
			html.selected();
		}
		html.close();
		html.append(String.valueOf(maxRowsDisplayed));
		html.optionEnd();

		html.newline();
		html.tabs(4);
		html.selectEnd();

		html.append(getMessages().getMessage(
				BuilderConstants.TOOLBAR_CRD));
	}

}
